#include <bits/stdc++.h>
using namespace std;

struct patient
{
    string id;
    int age;
};

void show(patient a[], int n)
{
    for(int i=0; i<n; i++)
    {
        cout << "id: " << a->id << ", age: " << a->age << endl;
    }
}

bool cmp(patient a, patient b)
{
    return a.age > b.age;
}

int main(int argc, char const *argv[])
{
    // 输入病人的数量
    int n;
    cin >> n;

    // 输入病人的信息
    patient a[n];
    patient elder[n];
    patient young[n];
    int p = 0;
    int q = 0;
    for(int i=0; i<n; i++)
    {
        cin >> a[i].id >> a[i].age;

        if(a[i].age >= 60)
        {
            elder[p++] = a[i];
            cout << elder[p-1].id << "," << elder[p-1].age << endl;
        }
        
        else 
        {
            young[q++] = a[i];
            cout << young[p-1].id << "," << young[p-1].age << endl;
        }
    }
    
    show(elder, p);
    show(young, q);

    // 对老年人按年龄进行排序
    sort(elder, &elder[p], cmp);

    // 输出排队就诊的id序列
    // A. 先输出老年人的序列
    for(int i=0; i<p-1; i++)
        cout << elder[i].id << endl;

    // B. 再输出非老年人的序列
    for(int i=0; i<q-1; i++)
        cout << young[i].id << endl;

    return 0;
}
